﻿using System;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using APP.HELPER.Data.Helper;
using APP.HELPER.Utils.CustomizeToolbox;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Controls;
using ComboBox = System.Windows.Forms.ComboBox;

namespace Dictionary.ERP
{
    public class HRM_DEPARTMENT
    {
        private string m_BranchCode;
        private string m_BranchName;
        private string m_DepartmentCode;
        private string m_DepartmentName;
        private string m_Description;
        private int m_FactQuantity;
        private string m_Phone;
        private int m_Quantity;

        public HRM_DEPARTMENT()
        {
            m_DepartmentCode = "";
            m_BranchCode = "";
            m_DepartmentName = "";
            m_Phone = "";
            m_Quantity = 0;
            m_FactQuantity = 0;
            m_Description = "";
        }

        public HRM_DEPARTMENT(string DepartmentCode, string BranchCode, string DepartmentName, string Phone,
            int Quantity, int FactQuantity, string Description)
        {
            m_DepartmentCode = DepartmentCode;
            m_BranchCode = BranchCode;
            m_DepartmentName = DepartmentName;
            m_Phone = Phone;
            m_Quantity = Quantity;
            m_FactQuantity = FactQuantity;
            m_Description = Description;
        }

        [Category("Foreign Key"), DisplayName("BranchCode")]
        public string BranchCode
        {
            get { return m_BranchCode; }
            set { m_BranchCode = value; }
        }

        [Category("Column"), DisplayName("BranchName")]
        public string BranchName
        {
            get { return m_BranchName; }
            set { m_BranchName = value; }
        }

        [DisplayName("DepartmentCode"), Category("Primary Key")]
        public string DepartmentCode
        {
            get { return m_DepartmentCode; }
            set { m_DepartmentCode = value; }
        }

        [Category("Column"), DisplayName("DepartmentName")]
        public string DepartmentName
        {
            get { return m_DepartmentName; }
            set { m_DepartmentName = value; }
        }

        [Category("Column"), DisplayName("Description")]
        public string Description
        {
            get { return m_Description; }
            set { m_Description = value; }
        }

        [Category("Column"), DisplayName("FactQuantity")]
        public int FactQuantity
        {
            get { return m_FactQuantity; }
            set { m_FactQuantity = value; }
        }

        [DisplayName("Phone"), Category("Column")]
        public string Phone
        {
            get { return m_Phone; }
            set { m_Phone = value; }
        }

        public string ProductName
        {
            get { return "Class HRM_DEPARTMENT"; }
        }

        public string ProductVersion
        {
            get { return "1.0.0.0"; }
        }

        [DisplayName("Quantity"), Category("Column")]
        public int Quantity
        {
            get { return m_Quantity; }
            set { m_Quantity = value; }
        }

        public void AddAllGridLookupEdit(GridLookUpEdit gridlookup)
        {
            var list = new DataTable();
            list = GetList();
            gridlookup.Properties.DataSource = list;
            gridlookup.Properties.DisplayMember = "DepartmentName";
            gridlookup.Properties.ValueMember = "DepartmentCode";
        }

        public void AddCombo(ComboBox combo)
        {
            GridCalendarControlLib.TableToComboBox(combo, GetList(), "DepartmentName", "DepartmentCode");
        }

        public void AddComboAll(ComboBox combo)
        {
            var list = new DataTable();
            list = GetList();
            DataRow row = list.NewRow();
            row["DepartmentCode"] = "All";
            row["DepartmentName"] = "Tất cả";
            list.Rows.InsertAt(row, 0);
            GridCalendarControlLib.TableToComboBox(combo, list, "DepartmentName", "DepartmentCode");
        }

        public void AddComboEdit(ComboBoxEdit combo)
        {
            var list = new DataTable();
            list = GetList();
            foreach (DataRow row in list.Rows)
            {
                combo.Properties.Items.Add(row["DepartmentName"]);
            }
        }

        public void AddGridLookupEdit(GridLookUpEdit gridlookup, string BranchCode)
        {
            var listByBranch = new DataTable();
            listByBranch = GetListByBranch(BranchCode);
            gridlookup.Properties.DataSource = listByBranch;
            gridlookup.Properties.DisplayMember = "DepartmentName";
            gridlookup.Properties.ValueMember = "DepartmentCode";
        }

        public void AddGridLookupEdit(GridLookUpEdit gridlookup, string BranchCode, string ValueMember)
        {
            var listByBranch = new DataTable();
            listByBranch = GetListByBranch(BranchCode);
            gridlookup.Properties.DataSource = listByBranch;
            gridlookup.Properties.DisplayMember = "DepartmentName";
            gridlookup.Properties.ValueMember = ValueMember;
        }

        public void AddImageComboBoxEdit(ImageComboBoxEdit imgCombo, string BranchCode)
        {
            imgCombo.Properties.Items.Clear();
            var listByBranch = new DataTable();
            listByBranch = GetListByBranch(BranchCode);
            var item = new ImageComboBoxItem
            {
                Description = "<Tất cả ph\x00f2ng ban>",
                Value = ""
            };
            imgCombo.Properties.Items.Add(item);
            foreach (DataRow row in listByBranch.Rows)
            {
                var item2 = new ImageComboBoxItem
                {
                    Description = row["DepartmentName"].ToString(),
                    Value = row["DepartmentCode"].ToString()
                };
                imgCombo.Properties.Items.Add(item2);
            }
            imgCombo.SelectedIndex = 0;
        }

        public string Delete()
        {
            string[] myParams = {"@DepartmentCode"};
            object[] myValues = {m_DepartmentCode};
            var helper = new SqlHelper();
            return helper.ExecuteNonQuery("HRM_DEPARTMENT_Delete", myParams, myValues);
        }

        public string Delete(string DepartmentCode)
        {
            string[] myParams = {"@DepartmentCode"};
            object[] myValues = {DepartmentCode};
            var helper = new SqlHelper();
            return helper.ExecuteNonQuery("HRM_DEPARTMENT_Delete", myParams, myValues);
        }

        public string Delete(SqlConnection myConnection, string DepartmentCode)
        {
            string[] myParams = {"@DepartmentCode"};
            object[] myValues = {DepartmentCode};
            var helper = new SqlHelper();
            return helper.ExecuteNonQuery(myConnection, "HRM_DEPARTMENT_Delete", myParams, myValues);
        }

        public string Delete(SqlTransaction myTransaction, string DepartmentCode)
        {
            string[] myParams = {"@DepartmentCode"};
            object[] myValues = {DepartmentCode};
            var helper = new SqlHelper();
            return helper.ExecuteNonQuery(myTransaction, "HRM_DEPARTMENT_Delete", myParams, myValues);
        }

        public bool Exist(string DepartmentCode)
        {
            bool hasRows = false;
            string[] myParams = {"@DepartmentCode"};
            object[] myValues = {DepartmentCode};
            var helper = new SqlHelper();
            SqlDataReader reader = helper.ExecuteReader("HRM_DEPARTMENT_Get", myParams, myValues);
            if (reader != null)
            {
                hasRows = reader.HasRows;
                reader.Close();
                helper.Close();
                reader = null;
            }
            return hasRows;
        }

        public string Get(string DepartmentCode)
        {
            string str = "";
            string[] myParams = {"@DepartmentCode"};
            object[] myValues = {DepartmentCode};
            var helper = new SqlHelper();
            SqlDataReader reader = helper.ExecuteReader("HRM_DEPARTMENT_Get", myParams, myValues);
            if (reader != null)
            {
                while (reader.Read())
                {
                    m_DepartmentCode = Convert.ToString(reader["DepartmentCode"]);
                    m_BranchCode = Convert.ToString(reader["BranchCode"]);
                    m_BranchName = Convert.ToString(reader["BranchName"]);
                    m_DepartmentName = Convert.ToString(reader["DepartmentName"]);
                    m_Phone = Convert.ToString(reader["Phone"]);
                    m_Quantity = Convert.ToInt32(reader["Quantity"]);
                    m_FactQuantity = Convert.ToInt32(reader["FactQuantity"]);
                    m_Description = Convert.ToString(reader["Description"]);
                    str = "OK";
                }
                reader.Close();
                helper.Close();
                reader = null;
            }
            return str;
        }

        public string Get(SqlConnection myConnection, string PositionCode)
        {
            string str = "";
            string[] myParams = {"@DepartmentCode"};
            object[] myValues = {DepartmentCode};
            var helper = new SqlHelper();
            SqlDataReader reader = helper.ExecuteReader(myConnection, "HRM_DEPARTMENT_Get", myParams, myValues);
            if (reader != null)
            {
                while (reader.Read())
                {
                    m_DepartmentCode = Convert.ToString(reader["DepartmentCode"]);
                    m_BranchCode = Convert.ToString(reader["BranchCode"]);
                    m_BranchName = Convert.ToString(reader["BranchName"]);
                    m_DepartmentName = Convert.ToString(reader["DepartmentName"]);
                    m_Phone = Convert.ToString(reader["Phone"]);
                    m_Quantity = Convert.ToInt32(reader["Quantity"]);
                    m_FactQuantity = Convert.ToInt32(reader["FactQuantity"]);
                    m_Description = Convert.ToString(reader["Description"]);
                    str = "OK";
                }
                reader.Close();
                helper.Close();
                reader = null;
            }
            return str;
        }

        public string Get(SqlTransaction myTransaction, string DepartmentCode)
        {
            string str = "";
            string[] myParams = {"@DepartmentCode"};
            object[] myValues = {DepartmentCode};
            var helper = new SqlHelper();
            SqlDataReader reader = helper.ExecuteReader(myTransaction, "HRM_DEPARTMENT_Get", myParams, myValues);
            if (reader != null)
            {
                while (reader.Read())
                {
                    m_DepartmentCode = Convert.ToString(reader["DepartmentCode"]);
                    m_BranchCode = Convert.ToString(reader["BranchCode"]);
                    m_BranchName = Convert.ToString(reader["BranchName"]);
                    m_DepartmentName = Convert.ToString(reader["DepartmentName"]);
                    m_Phone = Convert.ToString(reader["Phone"]);
                    m_Quantity = Convert.ToInt32(reader["Quantity"]);
                    m_FactQuantity = Convert.ToInt32(reader["FactQuantity"]);
                    m_Description = Convert.ToString(reader["Description"]);
                    str = "OK";
                }
                reader.Close();
                helper.Close();
                reader = null;
            }
            return str;
        }

        public DataTable GetList()
        {
            var helper = new SqlHelper();
            return helper.ExecuteDataTable("HRM_DEPARTMENT_GetList");
        }

        public DataTable GetListByBranch(string BranchCode)
        {
            string[] myParams = {"@BranchCode"};
            object[] myValues = {BranchCode};
            var helper = new SqlHelper();
            return helper.ExecuteDataTable("HRM_DEPARTMENT_GetListByBranch", myParams, myValues);
        }

        public string Insert()
        {
            string[] myParams =
            {
                "@DepartmentCode", "@BranchCode", "@DepartmentName", "@Phone", "@Quantity",
                "@FactQuantity", "@Description"
            };
            object[] myValues =
            {
                m_DepartmentCode, m_BranchCode, m_DepartmentName, m_Phone, m_Quantity, m_FactQuantity,
                m_Description
            };
            var helper = new SqlHelper();
            return helper.ExecuteNonQuery("HRM_DEPARTMENT_Insert", myParams, myValues);
        }

        public string Insert(SqlTransaction myTransaction)
        {
            string[] myParams =
            {
                "@DepartmentCode", "@BranchCode", "@DepartmentName", "@Phone", "@Quantity",
                "@FactQuantity", "@Description"
            };
            object[] myValues =
            {
                m_DepartmentCode, m_BranchCode, m_DepartmentName, m_Phone, m_Quantity, m_FactQuantity,
                m_Description
            };
            var helper = new SqlHelper();
            return helper.ExecuteNonQuery(myTransaction, "HRM_DEPARTMENT_Insert", myParams, myValues);
        }

        public string Insert(string DepartmentCode, string DepartmentName, int Quantity, int FactQuantity,
            string Description)
        {
            string[] myParams =
            {
                "@DepartmentCode", "@BranchCode", "@DepartmentName", "@Phone", "@Quantity",
                "@FactQuantity", "@Description"
            };
            object[] myValues = {DepartmentCode, BranchCode, DepartmentName, Phone, Quantity, FactQuantity, Description};
            var helper = new SqlHelper();
            return helper.ExecuteNonQuery("HRM_DEPARTMENT_Insert", myParams, myValues);
        }

        public string Insert(SqlConnection myConnection, string DepartmentCode, string BranchCode, string DepartmentName,
            string Phone, int Quantity, int FactQuantity, string Description)
        {
            string[] myParams =
            {
                "@DepartmentCode", "@BranchCode", "@DepartmentName", "@Phone", "@Quantity",
                "@FactQuantity", "@Description"
            };
            object[] myValues = {DepartmentCode, BranchCode, DepartmentName, Phone, Quantity, FactQuantity, Description};
            var helper = new SqlHelper();
            return helper.ExecuteNonQuery(myConnection, "HRM_DEPARTMENT_Insert", myParams, myValues);
        }

        public string Insert(SqlTransaction myTransaction, string DepartmentCode, string BranchCode,
            string DepartmentName, string Phone, int Quantity, int FactQuantity, string Description)
        {
            string[] myParams =
            {
                "@DepartmentCode", "@BranchCode", "@DepartmentName", "@Phone", "@Quantity",
                "@FactQuantity", "@Description"
            };
            object[] myValues = {DepartmentCode, BranchCode, DepartmentName, Phone, Quantity, FactQuantity, Description};
            var helper = new SqlHelper();
            return helper.ExecuteNonQuery(myTransaction, "HRM_DEPARTMENT_Insert", myParams, myValues);
        }

        public string NewID()
        {
            return SqlHelper.GenCode("HRM_DEPARTMENT", "DepartmentCode", "PB");
        }

        public string Update()
        {
            string[] myParams =
            {
                "@DepartmentCode", "@BranchCode", "@DepartmentName", "@Phone", "@Quantity",
                "@FactQuantity", "@Description"
            };
            object[] myValues =
            {
                m_DepartmentCode, m_BranchCode, m_DepartmentName, m_Phone, m_Quantity, m_FactQuantity,
                m_Description
            };
            var helper = new SqlHelper();
            return helper.ExecuteNonQuery("HRM_DEPARTMENT_Update", myParams, myValues);
        }

        public string Update(string DepartmentCode, string BranchCode, string DepartmentName, string Phone, int Quantity,
            int FactQuantity, string Description)
        {
            string[] myParams =
            {
                "@DepartmentCode", "@BranchCode", "@DepartmentName", "@Phone", "@Quantity",
                "@FactQuantity", "@Description"
            };
            object[] myValues = {DepartmentCode, BranchCode, DepartmentName, Phone, Quantity, FactQuantity, Description};
            var helper = new SqlHelper();
            return helper.ExecuteNonQuery("HRM_DEPARTMENT_Update", myParams, myValues);
        }

        public string Update(SqlConnection myConnection, string DepartmentCode, string BranchCode, string DepartmentName,
            string Phone, int Quantity, int FactQuantity, string Description)
        {
            string[] myParams =
            {
                "@DepartmentCode", "@BranchCode", "@DepartmentName", "@Phone", "@Quantity",
                "@FactQuantity", "@Description"
            };
            object[] myValues = {DepartmentCode, BranchCode, DepartmentName, Phone, Quantity, FactQuantity, Description};
            var helper = new SqlHelper();
            return helper.ExecuteNonQuery(myConnection, "HRM_DEPARTMENT_Update", myParams, myValues);
        }

        public string Update(SqlTransaction myTransaction, string DepartmentCode, string BranchCode,
            string DepartmentName, string Phone, int Quantity, int FactQuantity, string Description)
        {
            string[] myParams =
            {
                "@DepartmentCode", "@BranchCode", "@DepartmentName", "@Phone", "@Quantity",
                "@FactQuantity", "@Description"
            };
            object[] myValues = {DepartmentCode, BranchCode, DepartmentName, Phone, Quantity, FactQuantity, Description};
            var helper = new SqlHelper();
            return helper.ExecuteNonQuery(myTransaction, "HRM_DEPARTMENT_Update", myParams, myValues);
        }
    }
}